---
import BaseLayout from "@/layouts/BaseLayout.astro";
import List from "./_List.astro";
import Pagination from "@/components/Pagination/Pagination.astro";
import { COLLECT_POSITION_MAP } from "@/constant/Collect";
import { getCollection } from "astro:content";
import GetClassNamePosts from "@/utils/pages/collect/GetClassNamePosts";
import StaticPathFactory from "@/utils/pages/collect/StaticPathFactory";
import cfg from "blog.config";

export async function getStaticPaths() {
  const res = StaticPathFactory(
    await getCollection("blog", ({ data }) => data.draft !== true)!,
    cfg.pages.collect!.PageArticleCount!
  );
  return res;
}

interface Params {
  class: keyof typeof COLLECT_POSITION_MAP;
  name: string;
  page: string;
}

const LIST_MAX_ITEM_COUNT = cfg.pages.collect!.PageArticleCount!,
  { class: classify, name, page } = Astro.params as unknown as Params,
  finalPage = parseInt(parseInt(page) > 0 ? page : "1"),
  PathItem = GetClassNamePosts(
    await getCollection("blog", ({ data }) => data.draft !== true),
    classify,
    name
  );

//   if (
//   Object.keys(COLLECT_POSITION_MAP).indexOf(classify) == -1 ||
//   !name ||
//   !PathItem ||
//   !PathItem.length ||
//   !page ||
//   parseInt(page) < 1
// )
//   Astro.redirect("/404");
---

<BaseLayout>
  <div class="list flex_center">
    <div class="white_module">
      <h2>
        {COLLECT_POSITION_MAP[classify]}
        <span>{name}</span> 下的所有文章
      </h2>
      <List data={PathItem!} currentIndex={finalPage} />
    </div>
    <Pagination
      currentPage={finalPage}
      maxPage={Math.ceil(PathItem!.length / LIST_MAX_ITEM_COUNT)}
      path={`/collect/${classify}/${name}`}
    />
  </div>
</BaseLayout>

<style lang="scss">
  .list {
    flex-flow: column nowrap;
    width: 70%;
    margin: 3rem auto;

    .white_module {
      margin-bottom: 1rem;
      h2 {
        text-align: center;

        span {
          text-decoration: underline;
        }
      }
    }
  }

  @media screen and (max-width: 992px) {
    .list {
      width: 95%;
    }
  }
</style>
